#!/bin/bash
if [ "$os" = "Android" ]; then
    echo "安卓原生是吧!不安虚拟机用锤子你"
    exit 1
fi
if ! command -v qsign; then
if [ -d /usr/local/bin ]; then
curl -o /usr/local/bin/qsign https://gitee.com/yunxiyuan/yxy/raw/master/qsign && chmod 777 /usr/local/bin/qsign
elif [ -d /data/data/com.termux ]; then
curl -o ${PREFIX}/bin/qsign https://gitee.com/yunxiyuan/yxy/raw/master/qsign && chmod 777 ${PREFIX}/bin/qsign
fi
fi

dir="/x"
dirsign="${dir}/sign"
signconf="${dirsign}/txlib/x/config.json"
DOWN_URL="https://gitee.com/skinnyli/qsign1.1.5/releases/download/qsign1.1.5"
ARCH=$(uname -m)
signpid=$(pgrep -f "${dirsign}")

function install_qsign {
    case "$ARCH" in
        aarch64)
            ARCH_URL="aarch64"
            ;;
        amd64|x86_64)
            ARCH_URL="x64"
            ;;
        *)
            echo "暂不支持$ARCH架构"
            exit 1
            ;;
    esac
if [ ! $(command -v jq) ] || [ ! $(command -v wget) ] || [ ! $(command -v unzip) ]; then
if command -v apt &> /dev/null; then
    apt install -y jq wget unzip
elif command -v yum &> /dev/null; then
    yum install -y jq wget unzip
elif command -v dnf &> /dev/null; then
    dnf install -y jq wget unzip
elif command -v pacman &> /dev/null; then
    pacman -Sy --noconfirm --needed jq wget unzip
elif command -v zypper &> /dev/null; then
    zypper install -y jq wget unzip
else
    echo "无法检测到系统使用的安装命令"
fi
fi
    if [ ! -d "${dir}" ]; then
        mkdir -p "${dir}"
    fi

    pushd "${dir}"

os=$(uname -o)
if [ "$os" = "Linux" ]; then
  java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
    if [[ -z "$java_version" ]]; then
        major_version=$(echo "$java_version" | awk -F '.' '{print $2}')
        if [[ $major_version -le 8 ]]; then
            wget -c "$DOWN_URL/zulu8.70.0.23-ca-jdk8.0.372-linux_$ARCH_URL.tar.gz"
            mkdir -p jdk8
            tar -zxvf "zulu8.70.0.23-ca-jdk8.0.372-linux_$ARCH_URL.tar.gz" -C jdk8 --strip-components=1
            rm -f "zulu8.70.0.23-ca-jdk8.0.372-linux_$ARCH_URL.tar.gz"
            export PATH="${dir}/jdk8/bin:$PATH"
            echo "export PATH=${dir}/jdk8/bin:$PATH" >> ~/.bashrc
            source ~/.bashrc
        fi
    fi
fi
    mkdir -p "${dirsign}"

    if [ ! -f "${dirsign}/README.md" ]; then
        wget -c "$DOWN_URL/unidbg-fetch-qsign-master.zip"
        unzip unidbg-fetch-qsign-master.zip
        cp -r unidbg-fetch-qsign-master/* "${dirsign}/"
        rm -f unidbg-fetch-qsign-master.zip
        rm -rf unidbg-fetch-qsign-master
    fi

    if [ ! -f "${dirsign}/bin/unidbg-fetch-qsign" ]; then
        wget -c "$DOWN_URL/qsign-1.1.5.zip"
        unzip qsign-1.1.5.zip
        cp -r unidbg-fetch-qsign-1.1.5/* "${dirsign}/"
        rm -f qsign-1.1.5.zip
        rm -rf unidbg-fetch-qsign-1.1.5
    fi

    if [ ! -f "${signconf}" ]; then
        cp -r "${dirsign}/txlib/8.9.63/" "${dirsign}/txlib/x/"
        jq --arg ip "127.0.0.1" '.server.host = $ip | .server.port = 8088 | .key = "114514"' "${signconf}" > confignew.json && mv confignew.json "${signconf}"
        echo -e "安装完成，正在启动，你的签名接口链接为\nhttp://127.0.0.1:8088/sign?key=114514"
        start_qsign
    fi
}

function start_qsign {
    if [ -z "$signpid" ]; then
        echo "启动qsign"
        pushd "${dirsign}"
        nohup bash bin/unidbg-fetch-qsign --basePath=txlib/x &
        echo -e "运行成功,你的签名链接为:"
        jq -r '.server.host, .server.port, .key' ${signconf} | while read host; read port; read key; do echo "http://$host:$port/sign?key=$key"; done
    else
        echo "程序还在运行中"
    fi
}

function stop_qsign {
    if [ ! -z "$signpid" ]; then
        echo "关闭qsign"
        kill -15 "$signpid"
    else
        echo "没有正在运行的qsign进程"
    fi
}

function restart_qsign {
    echo "重启qsign"
        kill -15 "$signpid"
    start_qsign
}

function switch_protocol {
    version=$(jq -r '.protocol.version' "${signconf}")
    echo "切换签名协议,当前使用的是$version"
    txdir=$(ls -d "${dirsign}/txlib/"*[0-9]* | awk -F'/' '{print $NF}')
    echo -e "当前拥有的协议有\n$txdir"
    read -p "请输入签名协议（默认为8.9.63）：" PROTOCOL

    # 判断变量是否为空
    if [ -z "$PROTOCOL" ]; then
        PROTOCOL="8.9.63"
    fi
    rm -rf ${dirsign}/txlib/x/
    cp -r "${dirsign}/txlib/$PROTOCOL/" "${dirsign}/txlib/x/" || echo "目录$PROTOCOL不存在"
    echo "切换完成" && echo "你的签名验证接口为："
    jq -r '.server.host, .server.port, .key' ${signconf} | while read host; read port; read key; do echo "http://${host}:${port}/sign?key=$key"; done
    restart_qsign
}

function modify_config {
    read -p "请输入ip地址：" ip
    read -p "请输入端口：" port
    read -p "请输入密钥：" key

    jq --arg ip "$ip" --arg port $port --arg key "$key" '.server.host = $ip | .server.port = ($port | tonumber) | .key = $key' "${signconf}" > confignew.json && mv confignew.json "${signconf}"
    echo -e "修改完成，请关闭后启动，你的签名接口链接为\nhttp://$ip:$prot/sign?key=$key"
}

echo "
启动脚本命令qsign
1. 安装qsign
2. 启动qsign
3. 关闭qsign
4. 重启qsign
5. 切换签名协议
6. 修改接口配置"

read -p "请输入执行的命令：" NUM

case "$NUM" in
    1)
        install_qsign
        ;;
    2)
        start_qsign
        ;;
    3)
        stop_qsign
        ;;
    4)
        restart_qsign
        ;;
    5)
        switch_protocol
        ;;
    6)
        modify_config
        ;;
    *)
        echo "请输入有效的数字"
        ;;
esac

exit 0